/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUILayer;
import ControlLayer.VareKatalogCtrl;
import ExceptionsPakke.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;


/**
 *
 * @author vildmand
 */
public class LagerTableModel extends AbstractTableModel
{
    private VareKatalogCtrl vareKatalogCtrl;
    private JFrame frame;
    private boolean DEBUG = false;

    private String[] columnNames = {"VareID","Varenavn","Beskrivelse","Leverandør","Lokation","Antal"};
    
    Object[][] data = {{null, "", "", "", "", null}, 
        {null, "", "", "", "", null},
        {null, "", "", "", "", null},
        {null, "", "", "", "", null},
        {null, "", "", "", "", null},
        {null, "", "", "", "", null}};

        /**
        * Konstruktør for objekter af klassen LagerTableModel
        */
        public LagerTableModel()
        {
            vareKatalogCtrl = new VareKatalogCtrl();
            frame = new JFrame();
        }
        
        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
        
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }

        public void setValueAt(Object value, int row, int col) {
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of "
                                   + value.getClass() + ")");
            }     
                data[row][col] = value;
                fireTableCellUpdated(row, col);

            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }

        private void printDebugData() {
            int numRows = getRowCount();
            int numCols = getColumnCount();

            for (int i=0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }
        
        /**
         * Metode der returnerer et to-dimensionelt array
         * @return data [][] Det to-dimensionelle array, der udgør tabellen
         */
        public Object[][] getData()
        {
            return data;
        }
        
        /**
         * Metode der nulstiller tabellen (to-dimensionelt array)
         */
        public void clear()
        {
            for(int i = 0; i < data.length; i++)
            {
                for(int j = 0; j < 6; j++)  
                {
                    data[i][j] = null;
                }
            }
            fireTableDataChanged();
        }
        
        /**
         * Metode der sætter data ind i et to-dimensionelt array (tabel-data)
         * @param vareNavn Navn eller delnavn på den søgte vare
         */
        public void indsaetITabel(String vareNavn)
        {
            try
            {
                for(int i = 0; i < vareKatalogCtrl.findVareBeskrivelse2(vareNavn).size(); i++)
                {   
                    
                        data[i][0] = vareKatalogCtrl.findVareBeskrivelse2(vareNavn).get(i).hentVareId();
                        data[i][1] = vareKatalogCtrl.findVareBeskrivelse2(vareNavn).get(i).hentNavn();
                        data[i][2] = vareKatalogCtrl.findVareBeskrivelse2(vareNavn).get(i).hentBeskrivelse();
                        data[i][3] = vareKatalogCtrl.findVareBeskrivelse2(vareNavn).get(i).hentLeverandoerNavn();
                        data[i][4] = vareKatalogCtrl.findVareBeskrivelse2(vareNavn).get(i).hentLokation();
                        data[i][5] = vareKatalogCtrl.findVareBeskrivelse2(vareNavn).get(i).arrayStoerrelse();  
                }
            }
            catch(NoSuchVareNavnException e)
            {
                JOptionPane.showMessageDialog(frame,
                e.toString());
            }
            fireTableDataChanged();  
        }
    }
